/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.ResultDataDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfListDTO;
import org.hotpotmaterial.codegenn2.dto.request.StringRequestDTO;
import org.hotpotmaterial.codegenn2.exception.CodeCommonException;
import org.hotpotmaterial.codegenn2.service.IProjectPluginService;
import org.hotpotmaterial.codegenn2.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hutool.core.io.FileUtil;
import io.swagger.annotations.Api;

/**
 * @author causer
 *
 */
@Controller
@RequestMapping("/hmteam/codegen/v1/project/plugins")
@Api(value = "模块插件Controller", tags = { "模块插件管理" })
public class ProjectPluginController {

	@Autowired
	private IProjectPluginService projectPluginService;

	/**
	 * 生成文件树
	 * 
	 * @param id
	 * @param password
	 * @return
	 */
	@PostMapping("/{id}")
	public ResponseEntity<ResultBaseDTO> postGenerateFileTree(@PathVariable Long id, Map<String, String> password) {
		return new ResponseEntity<>(
				ResultDataDTO.<String>builder().data(projectPluginService.generateFileTree(id, password))
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 获取文件夹下面所有文件名称
	 * 
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{id}/dir_files")
	public ResponseEntity<ResultBaseDTO> postDirFiles(@RequestBody StringRequestDTO data) {
		ResultBaseDTO result = ResultOfListDTO.<String>builder().data(projectPluginService.getFileList(data.getParam()))
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	/**
	 * 判断文件是否为目录
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/{id}/is_dir")
	public ResponseEntity<ResultBaseDTO> postFileIsDir(@RequestBody StringRequestDTO param) {
		return new ResponseEntity<>(
				ResultDataDTO.<String>builder().data(projectPluginService.isDirectory(param.getParam()))
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 获取文件内容
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/{id}/file_content")
	public ResponseEntity<ResultBaseDTO> postFileContent(@RequestBody StringRequestDTO param) {
		return new ResponseEntity<>(
				ResultDataDTO.<String>builder().data(projectPluginService.getFileContent(param.getParam()))
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

	/**
	 * 下载文件
	 * @param param
	 * @param res
	 */
	@PostMapping("/{id}/file_download")
	public void postFileDownload(@RequestBody StringRequestDTO param,
			HttpServletResponse res) {
		try {
			String fileName = FileUtil.getName(param.getParam());
			byte[] bytes = projectPluginService.downloadFile(param.getParam());
			RequestUtil.setResponseHeader(res, fileName);
			OutputStream os = res.getOutputStream();
			os.write(bytes);
			os.flush();
			os.close();
		} catch (IOException e1) {
			throw new CodeCommonException("IO异常");
		}
	}
	
	/**
	 * 下载压缩后文件树
	 * @param param
	 * @param res
	 */
	@PostMapping("/{id}/zip_download")
	public void postZipDownload(@RequestBody StringRequestDTO param,
			HttpServletResponse res) {
		try {
			File zipFile = projectPluginService.downloadZip(param.getParam());
			byte[] bytes = FileUtil.readBytes(zipFile);
			RequestUtil.setResponseHeader(res, FileUtil.getName(zipFile));
			OutputStream os = res.getOutputStream();
			os.write(bytes);
			os.flush();
			os.close();
		} catch (IOException e1) {
			throw new CodeCommonException("IO异常");
		}
	}
}
